package com.example.zg.zg_312_ordering_new.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.activity.LoginActivity;
import com.example.zg.zg_312_ordering_new.adapter.CollectionFoodAdapter;
import com.example.zg.zg_312_ordering_new.beans.CollectionList_Food;
import com.example.zg.zg_312_ordering_new.listener.ListListener;
import com.example.zg.zg_312_ordering_new.model.CollectionFoodListModel;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class CollectFragment_food extends Fragment {

    private List<CollectionList_Food> List;
    RecyclerView recyclerView;
    CollectionFoodAdapter viewAdapter;
    SwipeRefreshLayout swipeRefreshLayout;
    int user_id;
    private int flag=1;
    private int shuaxin_flag=0;

    ListListener Listener = new ListListener() {

        @Override
        public void onResponse(List list) {
            List = list;
            viewAdapter.setDate(List);
            if(shuaxin_flag==1){
                shuaxin_flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };

    public void setUser_id(int user_id){
        this.user_id=user_id;
    }

    public CollectFragment_food() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_collection_fragment_2, container, false);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        viewAdapter = new CollectionFoodAdapter();

        recyclerView = view.findViewById(R.id.collect_recycler2);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);

//        //添加自己设计的分割线
//        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL);
//        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(getContext(),R.drawable.divider));
//        recyclerView.addItemDecoration(dividerItemDecoration);

        swipeRefreshLayout = view.findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorBlue),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorBlue));

        swipeRefreshLayout.setProgressViewOffset(true,30,60);

//        swipeRefreshLayout.setRefreshing(false);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                shuaxin_flag=1;
                CollectionFoodListModel collectionFoodListModel = new CollectionFoodListModel();
                collectionFoodListModel.getResultList(user_id,flag,Listener);

            }
        });


        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(viewAdapter);

        user_id=Integer.parseInt(LoginActivity.User_id);

        CollectionFoodListModel collectionFoodListModel = new CollectionFoodListModel();
        collectionFoodListModel.getResultList(user_id,flag,Listener);

    }
}

